package io.terminus.paas.profiler.service.impl

import com.alibaba.dubbo.common.Constants
import com.alibaba.dubbo.common.URL
import io.terminus.paas.profiler.AbstractProfilerService
import io.terminus.paas.profiler.utils.sync.SyncUtils
import io.terminus.paas.profiler.model.Consumer
import io.terminus.paas.profiler.service.ConsumerService
import org.springframework.stereotype.Service
import java.util.*

/**
 * Created by gengrong on 16/3/18.
 */
@Service
class ConsumerServiceImpl : ConsumerService, AbstractProfilerService() {
    override fun findConsumerByServiceId(registryAddress: String, serviceId: String): List<Consumer> {
        return SyncUtils.url2ConsumerList(findConsumerUrlByService(registryAddress, serviceId))
    }

    override fun getDetailByConsumerId(registryAddress: String, consumerId: Long): Consumer {
        throw UnsupportedOperationException()
    }

    override fun enableConsumer(registryAddress: String, consumerId: Long) {
        throw UnsupportedOperationException()
    }

    override fun disableConsumer(registryAddress: String, consumerId: Long) {
        throw UnsupportedOperationException()
    }

    fun findConsumerUrlByService(registryAddress: String, service: String): Map<Long, URL> {
        val filter = HashMap<String, String>()
        filter.put(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY)
        filter.put(SyncUtils.SERVICE_FILTER_KEY, service)
        return SyncUtils.filterFromCategory(getRegistryCache(registryAddress), filter)
    }

}